package handler

import (
	"net/http"
	"strconv"

	"github.com/iriver/ai-offer/backend/internal/logic"
	"github.com/iriver/ai-offer/backend/internal/svc"
	"github.com/iriver/ai-offer/backend/internal/types"
	"github.com/zeromicro/go-zero/rest/httpx"
)

func ListUsersHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var req types.ListUserRequest

		// 从URL参数中获取查询条件
		pageStr := r.URL.Query().Get("page")
		if pageStr != "" {
			page, err := strconv.ParseInt(pageStr, 10, 64)
			if err == nil && page > 0 {
				req.Page = page
			}
		}

		pageSizeStr := r.URL.Query().Get("pageSize")
		if pageSizeStr != "" {
			pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
			if err == nil && pageSize > 0 && pageSize <= 100 {
				req.PageSize = pageSize
			}
		}

		req.Role = r.URL.Query().Get("role")

		l := logic.NewListUsersLogic(r.Context(), svcCtx)
		resp, err := l.ListUsers(&req)
		if err != nil {
			httpx.Error(w, err)
		} else {
			httpx.OkJson(w, resp)
		}
	}
}
